package com.service.Authorization.controller;

import com.service.Authorization.dto.*;
import com.service.Authorization.entity.PortalUser;
import com.service.Authorization.entity.ProgrammeInfoExamine;
import com.service.Authorization.service.ManageProgrammeInfoService;
import com.service.Authorization.service.PortalProgrammeInfoService;
import com.service.Authorization.service.PortalUserService;
import com.service.Authorization.service.ProgrammeInfoExamineService;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.service.config.constant.Constant.*;

/**
 * 方案信息表(ProgrammeInfoExamine)表控制层
 *
 * @author makejava
 * @since 2021-08-10 16:14:31
 */
@RestController
@RequestMapping("programmeInfoExamine")
@Api(value = "方案信息审核接口", tags = "方案信息审核接口")
public class ProgrammeInfoExamineController {
    /**
     * 服务对象
     */
    @Resource
    private PortalProgrammeInfoService portalProgrammeInfoService;
    /**
     * 服务对象
     */
    @Resource
    private ProgrammeInfoExamineService programmeInfoExamineService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private PortalUserService portalUserService;
    /**
     * 服务对象
     */
    @Resource
    private ManageProgrammeInfoService manageProgrammeInfoService;
    /**
     * 提交审核，正式表的状态为审核中
     * @param submitProgrammeInfoExamineDto
     * @return
     */
    @ApiOperation(value = "租户提交审核，正式表的状态为审核中")
    @PostMapping(value = "/updateProgrammeInfoConfig",produces="application/json;charset=UTF-8")
    public Result updateProgrammeInfoConfig(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                            @RequestBody SubmitProgrammeInfoExamineDto submitProgrammeInfoExamineDto) throws Exception {
            if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
                String userId = UserTokenManager.getUserId(cloudAuthToken);
                if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                    return programmeInfoExamineService.updateProgrammeInfoConfig(userId,submitProgrammeInfoExamineDto);
                } else {
                    return Result.failure(USERNAME_Off_SITE, usernameOffSite);
                }
            }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 查询用户所拥有的方案,
     * @return
     */
    @ApiOperation(value = "查询租户所拥有的方案")
    @GetMapping(value = "/queryManageProgrammeInfo")
    public Result queryManageProgrammeInfo(@RequestHeader(value = "cloud-Auth-Token",required = false)
                                               @ApiParam("token")String cloudAuthToken,
                                           @RequestParam(value = "programmeInfoId",required = false) @ApiParam("方案id") String programmeInfoId,
                                           @RequestParam(value = "type",required = false) @ApiParam("0--普通 1--企业 2--微信") Integer type) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                PortalUser portalUser = new PortalUser();
                portalUser.setManageId(userId);
                List<ScreenUserDto> screenUserDtos = portalUserService.queryAll(userId);
                if (screenUserDtos == null || screenUserDtos.size() == 0 ){
                    return Result.failure(10013,"您的账号还未关联华为云管理账号，请进入大屏后台进行关联");
                }
                if (programmeInfoId != null) { // 方案id存在查询单个
                    return programmeInfoExamineService.queryManageProgrammeInfo(userId,programmeInfoId,type);
                }
                return programmeInfoExamineService.queryManageProgrammeInfo(userId,"",type);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }

    /**
     * portal页面查询,
     * @param programmeInfoId
     * @return
     */
    @ApiOperation(value = "portal页面查询")
    @GetMapping(value = "/queryPortalHtml")
    public Result queryPortalHtml( @RequestParam(value = "programmeInfoId")
                                                @ApiParam("方案id") String programmeInfoId) throws Exception {
        return manageProgrammeInfoService.queryManageProgrammeInfo("",programmeInfoId);
    }
    /**
     * 用户新增方案
     * @param addProgrammeInfoExamineDto
     * @return
     */
    @ApiOperation(value = "租户新增方案")
    @PostMapping(value = "/addManageProgrammeInfo",produces="application/json;charset=UTF-8")
    public Result addManageProgrammeInfo(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                         @RequestBody AddProgrammeInfoExamineDto addProgrammeInfoExamineDto
                                         ) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                return programmeInfoExamineService.addManageProgrammeInfo(userId,addProgrammeInfoExamineDto);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 用户新增方案
     * @param deleteProgrammeInfoExamineDto
     * @return
     */
    @ApiOperation(value = "租户删除方案")
    @PostMapping(value = "deleteProgramme",produces="application/json;charset=UTF-8")
    public Result  deleteProgramme(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                         @RequestBody DeleteProgrammeInfoExamineDto deleteProgrammeInfoExamineDto){
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                programmeInfoExamineService.deleteById(deleteProgrammeInfoExamineDto.getId());
                return Result.ok();
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 用户撤销审核
     * @param programmeId
     * @return
     */
    @ApiOperation(value = "租户撤销审核")
    @GetMapping(value = "/cancelProgrammeInfoConfig")
    public Result cancelProgrammeInfoConfig(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                       @RequestParam(value = "programmeId") String programmeId) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                return programmeInfoExamineService.cancelProgrammeInfoConfig(programmeId);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 后台审核完毕，用户操作提交配置应用此方案
     * @param applicationProgrammeInfoDto
     * @return
     */
    @ApiOperation(value = "审核完毕，用户操作提交配置应用此方案")
    @PostMapping(value = "/putIssue")
    public Result putIssue(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                           @RequestBody ApplicationProgrammeInfoDto applicationProgrammeInfoDto) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                return programmeInfoExamineService.putIssue(applicationProgrammeInfoDto);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 查看URL
     * @param programmeInfoId
     * @return
     */
    @ApiOperation(value = "查看URL")
    @GetMapping(value = "/getUrl")
    public Result getUrl(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                           @RequestParam(value = "programmeInfoId")
                               @ApiParam("方案id") String programmeInfoId) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                return programmeInfoExamineService.getUrl(userId,programmeInfoId);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 云管账号和方案绑定
     * @param bindAccount
     * @return
     */
    @ApiOperation(value = "云管账号和方案绑定")
    @PostMapping(value = "/bindAccountAndProgramme")
    public Result bindAccountAndProgramme(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                         @RequestBody BindAccount bindAccount) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) { // 密码修改
                return portalProgrammeInfoService.bindAccountAndProgramme(userId,bindAccount);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 管理员查询审核中信息
     * @param flag
     * @return
     */
    @ApiOperation(value = "运营管理员查询审核中信息")
    @GetMapping(value = "/queryRecordInfoS")
    public Result queryRecordInfoS(@RequestHeader(value = "Token",required = false) String cloudAuthToken,
                                   @RequestParam(value = "flag",required = false)  @ApiParam("1--代表查询审核中 2--审核失败和审核成功 null或者其它数字查询所有") String flag) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            if (redisUtil.exist(cloudAuthToken)) { // 密码修改
                return programmeInfoExamineService.queryRecordInfoS(new ProgrammeInfoExamine(),flag);
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
    /**
     * 开始审核
     *
     * @param cloudAuthToken
     * @return 单条数据
     */
    @PostMapping(value = "verifyShopInfo",produces="application/json;charset=UTF-8")
    @ApiOperation(value = "运营开始审核")
    @ResponseBody
    public Result verifyShopInfo(@RequestHeader(value = "token",required = false) String cloudAuthToken,
                                 @RequestParam(value = "flag")  @ApiParam("1--全通过（代表审核成功） 2--不全通过（代表审核失败）") String flag,
                                 @RequestBody VerifyProgrammeInfoDto verifyProgrammeInfoDto ) throws Exception {
        if (redisUtil.exist(cloudAuthToken)) {
            programmeInfoExamineService.verifyShopInfo(redisUtil.getValue(cloudAuthToken),flag,verifyProgrammeInfoDto);
            return Result.ok();
        }
        return Result.failure(USER_NOT_LOGIN,userNotLogin);
    }
}
